﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package eu.orangeflash.webapis.xmlrpc.utils {
    import flash.utils.*;

    public class Serializer implements IXMLRPCSerializer {

        public const INT:String = "int";
        public const DOUBLE:String = "double";
        public const STRUCT:String = "struct";
        public const STRING:String = "string";
        public const NULL:String = "null";
        public const BASE64:String = "base64";
        public const BOOL:String = "boolean";
        public const DATE:String = "dateTime.iso8601";
        public const ARRAY:String = "array";

        public function Serializer(){
            super();
        }
        
        private function createParam(arg:*):XML
        {
            var type:String;
            var result:XML;
            type = getType(arg);
            result = <param/>
            ;
            if (((!((type == STRUCT))) && (!((type == ARRAY))))){
                result.appendChild(createMember(createSimpleType(type, arg)));
            } else {
                if (type == STRUCT){
                    result.appendChild(createMember(createStruct(arg)));
                } else {
                    if (type == ARRAY){
                        result.appendChild(createMember(createArray(arg)));
                    };
                };
            };
            return (result);
        }
        public function createMethod(methodName:String, params:Array=null):String{
            var result:XML;
            var methodParams:XMLList;
            var arg:*;
            result = <methodCall>
			   <methodName/>
			   <params/>
			</methodCall>
            ;
            result.methodName.appendChild(methodName);
            methodParams = result.params;
            for each (arg in params) {
                methodParams.appendChild(createParam(arg));
            };
            return (result.toXMLString());
        }
        private function createArray(value:Array):XML{
            var result:XML;
            var item:*;
            var type:String;
            result = <array>
					<data>
					</data>
				</array>
            ;
            for each (item in value) {
                type = getType(item);
                if (((!((type == STRUCT))) && (!((type == ARRAY))))){
                    result.data.appendChild(createMember(createSimpleType(type, item)));
                } else {
                    if (type == STRUCT){
                        result.data.appendChild(createMember(createStruct(item)));
                    } else {
                        if (type == ARRAY){
                            result.data.appendChild(createMember(createArray(item)));
                        };
                    };
                };
            };
            return (result);
        }
        private function createSimpleType(type:String, value:*):XML{
            var result:XML;
            var now:Date;
            result = <type/>
            ;
            result.setName(type);
            if (type != DATE){
                result.appendChild(value);
            } else {
                now = new Date();
                result.appendChild(((((((((now.getFullYear().toString() + (now.getMonth() + 1).toString()) + now.getDate().toString()) + "T") + now.getHours().toString()) + ":") + now.getMinutes().toString()) + ":") + now.getSeconds().toString()));
            };
            return (result);
        }
        private function getType(value:*):String{
            var type:String;
            var result:String;
            type = describeType(value).@name;
            switch (type){
                case "Array":
                    result = ARRAY;
                    break;
                case "Boolean":
                    result = BOOL;
                    break;
                case "Date":
                    result = DATE;
                    break;
                case "Number":
                    result = DOUBLE;
                    break;
                case "int":
                    result = INT;
                    break;
                case "String":
                    result = STRING;
                    break;
                default:
                    result = STRUCT;
            };
            return (result);
        }
        private function createMember(member:XML, name:String=null):XML{
            var result:XML;
            result = <xml/>
            ;
            if (name != null){
                result.setName("member");
                result.appendChild(<name/>
                );
                result.name.appendChild(name);
                result.appendChild(<value/>
                );
                result.value.appendChild(member);
            } else {
                result.setName("value");
                result.appendChild(member);
            };
            return (result);
        }
        private function createStruct(value:Object):XML{
            var result:XML;
            var itemName:String;
            var item:*;
            var type:String;
            result = <struct/>
            ;
            for (itemName in value) {
                item = value[itemName];
                type = getType(item);
                if (((!((type == STRUCT))) && (!((type == ARRAY))))){
                    result.appendChild(createMember(createSimpleType(type, item), itemName));
                } else {
                    if (type == STRUCT){
                        result.appendChild(createMember(createStruct(item), itemName));
                    } else {
                        if (type == ARRAY){
                            result.appendChild(createMember(createArray(item), itemName));
                        };
                    };
                };
            };
            return (result);
        }

    }
}//package eu.orangeflash.webapis.xmlrpc.utils 
